<template>
	<view class="cataloguelist">
		<view class="listhead">
			<view class="listhead-left">
				共{{all}}章
			</view>
		</view>
		<view class="listcontent">
			<view class="list-item" v-for="(i,ind) in arr" :key="ind" @click="switchcontent(i.uuid,ind)">
				<rich-text :nodes="i.title"></rich-text>
			</view>
		</view>
	</view>
</template>

<script>
	import {getSectionList} from "../../api/index.js"
	export default {
		data() {
			return {
				bookid:"",
				all:"",
				arr:"",
				bookimg:"",
				booksummary:"",
				booktitle:""
			}
		},
		onLoad(options){
			console.log(options);
			this.bookid=options.bookid
			this.bookimg=options.bookimg
			console.log(this.bookimg);
			this.booksummary=options.booksummary
			this.booktitle=options.booktitle
			console.log("options:",options.bookid);
			console.log(this.bookid);
			uni.showLoading({
			     title: '正在加载...',
			});
			getSectionList(options.bookid).then(res=>{
				
				console.log(res.data.catalog)
				this.all=res.data.catalog.length
				let newArr=[]
				for(let i=0;i<res.data.catalog.length;i++){
					newArr.push({title:res.data.catalog[i].title.trim(),uuid:res.data.catalog[i].uuid});
				}
				console.log(newArr);
				this.arr=newArr
				uni.hideLoading();
			})
		},
		methods: {
			switchcontent(uuid,ind){
				console.log(this.bookid,uuid,ind);
				let key=uni.getStorageSync("key")
				let book_img=this.bookimg
				let book_id=this.bookid
				let book_title=this.booktitle
				let book_summary=this.booksummary
				if(key.length!=0){
					let book_img=this.bookimg
					let book_id=this.bookid
					let book_title=this.booktitle
					let book_summary=this.booksummary
					uniCloud.callFunction({
						name:"where_read",
						data:{
							user_id:key,
							book_id
						},
						success:res=>{
							console.log(res);
							if(res.result.data.length!=0){
								// console.log("已经阅读过");
								//删除之前的记录再添加
								uniCloud.callFunction({
									name:"del_readhistory",
									data:{
										user_id:key,
										book_id
									},
									success:res=>{
										console.log(res);
										if(res.result.deleted==1){
											uniCloud.callFunction({
												name:"read_history",
												data:{
													user_id:key,
													book_img,
													book_id,
													book_title,
													book_summary
												},
												success:res=>{
													console.log(res);
												}
											})
										}
									}
								})
								//查询记录
								uniCloud.callFunction({
									name:"where_novel_records",
									data:{
										user_id:key,
										book_id
									},
									success:res=>{
										console.log(res,111);
										if(res.result.data.length>0){
											uniCloud.callFunction({
												name:"update_novel_records",
												data:{
													user_id:key,
													book_id,
													chapter_index:ind,
													book_uuid:uuid
												},
												success:res=>{
													console.log(res);
												}
											})
										}
										// book_id: "ts_2c9b78ad9af94a99b44d162abd528842_4"
										// book_uuid: "1a129168b99d435e8a1aee0a2f603673_4"
										// chapter_index: 0
										// user_id: "9294_6850567943940724"
										// _id: "6447b5e328064a75870682b2"
									}
								})
							}else{
								// console.log("还没看过");
								let book_img=this.bookimg
								let book_id=this.bookid
								let book_title=this.booktitle
								let book_summary=this.booksummary
								uniCloud.callFunction({
									name:"read_history",
									data:{
										user_id:key,
										book_img,
										book_id,
										book_title,
										book_summary
									},
									success:res=>{
										console.log(res);
									}
								})
								//添加记录
								uniCloud.callFunction({
									name:"add_novel_records",
									data:{
										user_id:key,
										book_id,
										book_uuid:uuid,
										chapter_index:ind
									},
									success:res=>{
										console.log(res);
									}
								})
							}
						}
					})
					
				}
				
				// console.log(book_img,book_id,book_title,book_summary);
				// console.log(uuid,book_id,ind,key);
				
				
				
				
				
				uni.navigateTo({
					url:`../fictioncontent/fictioncontent?bookid=${this.bookid}&uuid=${uuid}&ind=${ind}`
				})
			}
			
		}
	}
</script>

<style lang="scss">
.cataloguelist{
	.listhead{
		width: 100%;
		height: 40px;
		line-height: 40px;
		box-shadow: 0 0 5px -2px black;
		font-size: 16px;
		box-sizing: border-box;
		padding: 0 13px;
		font-weight: bold;
	}
	.listcontent{
		.list-item{
			display: flex;
			box-sizing: border-box;
			padding: 13px ;
			border-bottom: 1px solid #ccc;
			font-size: 16px;
		}
	}
}
</style>
